/**===========================================================================================
# mod_otminislideshow   OT Mini Slideshow Module For Joomla 1.7.0
#=============================================================================================
# author                OmegaTheme.com
# copyright             Copyright (C) 2011 OmegaTheme.com. All rights reserved.
# @license              http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Website               http://omegatheme.com
# Technical support     Forum - http://omegatheme.com/forum/
#=============================================================================================*/

/**--------------------------------------------------------------------------------------------
* file:           otmss_moo13.js 1.7.0 00002, 12 Dec 2011 15:00:00Z OmegaTheme:Linh $
* package:        OT Mini Slideshow module
* description:    js file mootools 1.3 native compatible
*---------------------------------------------------------------------------------------------*/

var MSS = new Class({
    //default options
    options: {
        showControls: true,
        showDuration: 5000,
        showToc: true,
        tocClass: 'toc',
        tocActiveClass: 'toc_active',
        tocRanId: ''
    },

    Implements: [Options,Events],
    initialize: function(container,elements,options) {
        this.container = document.id(container);
        this.elements = document.getElements(elements);
        this.options = Object.merge(this.options, options);
        this.currentIndex = 0;
        this.interval = '';
        
        this.toc = new Array();
        
        if(this.options.showToc == true)
        {
            var tocContainer = document.id('tc_'+this.options.tocRanId);
            this.elements.each(function(el,i){
                var toc_elm = new Element('span',{
                    text: i+1,
                    'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
                    events: {
                        click: function(e) {
                            if(e) e.stop();
                            this.mstop();
                            this.show(i);
                        }.bind(this)
                    }
                });
                this.toc.include(toc_elm);
                toc_elm.inject(tocContainer);
                
                if(i > 0) el.set('opacity',0);
            },this);
        }

        if(this.options.showControls == true) {
            this.createControls();
        }

        this.container.addEvents({
            mouseenter: function() { this.mstop(); }.bind(this),
            mouseleave: function() { this.mstart(); }.bind(this)
        });

    },
    show: function(toc_selected) {
        this.elements[this.currentIndex].fade('out');
        if(this.options.showToc == true) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
        this.currentIndex = ((toc_selected != null && toc_selected >= 0 && toc_selected < this.elements.length) ? toc_selected : (this.currentIndex >= 0 && this.currentIndex < (this.elements.length - 1) ? this.currentIndex + 1 : 0));
        this.elements[this.currentIndex].fade('in');
        if(this.options.showToc == true) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
    },

    mstart: function() {
        var toc_selected = null;
        this.interval = this.show.periodical(this.options.showDuration, this, [toc_selected]);
    },

    mstop: function() {
        clearInterval(this.interval);
    },

    /* Create Next and Previous button */
    createControls: function() {
        var next = new Element('span',{
            id: 'next',
            text: ' ',
            events: {
                click: function(e) {
                    if(e) e.stop();
                    this.mstop(); 
                    this.show();
                }.bind(this)
            }
        }).inject(this.container);
        var previous = new Element('span',{
            id: 'previous',
            text: ' ',
            events: {
                click: function(e) {
                    if(e) e.stop();
                    this.mstop();
                    this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
                }.bind(this)
            }
        }).inject(this.container);
    }
});
